package com.mall.common.valid;

//import jakarta.validation.ConstraintValidator;
//import jakarta.validation.ConstraintValidatorContext;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import lombok.extern.slf4j.Slf4j;
import java.util.HashSet;
import java.util.Set;

/**
 * 血的教训-切记 *
 * 注意：
 * 导入的包要正确一定是这俩：javax
 * import javax.validation.Constraint;
 * import javax.validation.Payload;
 * ---------------------------------
 * 千万别导错了,这俩不生效有问题：jakarta
 * import jakarta.validation.Constraint;
 * import jakarta.validation.Payload;
 */
@Slf4j
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
    private Set<Integer> set = new HashSet<>();

    /**
     * 初始化方法,传入的set遍历放入value数组值
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        //log.error("进入自定义校验注解");
        int[] value = constraintAnnotation.value();
        for (int val:value) {
            set.add(val);
        }
    }
    /**
     * 判断是否校验成功
     * @param value 需要校验的值
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        //log.error("进入自定义校验注解判断");
        return set.contains(value);
    }

}
